$(function(){

    //移除占位div
    setTimeout(function(){
        $("#placeholder_div").remove();
    },1000);

    //最开始渲染轮播图
    // 我的部门////////////////////////////
    //设置轮播图图片路径
    layui.use('carousel', function(){
        var carousel = layui.carousel;
        //建造实例
        carousel.render({
            elem: '#Shuffling_div',
            width: '600px', //设置容器宽度
            height: '374px',
            arrow: 'hover', //始终显示箭头
        });
    });

    //获取部门信息
    $.post(
        "/departmentInformServlet",
        {},
        function(date){
            $("#department_name").text(date.department_name);
            $("#department_introduction").text(date.department_introduction);
        },"json"
    )

    // 获取小组信息
    $.post(
        "/groupInformServlet",
        {},
        function(date){
            $("#group_name").text(date.group_name);
            $("#group_introduction").text(date.group_introduction);
        },"json"
    )

    //获取同组的同事信息
    $.post(
        "/queryColleaguesServlet",
        function (data){
            console.log(data);
            for (var i = 0; i < data.length; i++) {
                var s = "<tr>"
                s += "<td>"+data[i].ename+"</td>";
                s += "<td>"+data[i].workingtime+"</td>"
                s += "<td>"+data[i].current_position+"</td>";
                s += "<tr>";
                $("#content3_tbody").append(s);
            }
        }
    );

    //导航栏的切换
    var flagBefore = 0;
    $("#main_content>ul>li:gt(0)").each(function (index2, element2) {
            $(element2).slideToggle("normal");
    });
    $("#nav_ul>li:gt(1):lt(7)").each(function(index1,element1) {
        //导航栏li被点击
        $(element1).click(function () {
            // alert(index1+"被点击")
            if(index1!=flagBefore){
                $("#main_content>ul>li").each(function (index2, element2) {
                    if (index2 == flagBefore) {
                        // alert("关掉"+index2);
                        $(element2).slideToggle("normal");
                    }
                });
                $("#main_content>ul>li").each(function (index2, element2) {
                    if (index2 == index1) {
                        // alert("打开"+index2);
                        $(element2).slideToggle("normal");
                        flagBefore = index1;
                    }
                })
            }
        })
    });

    //AJAX头像图片名字请求
    $.post(
        "/headPortraitServlet",
        {},
        function (data){
            $("#userPortrait").prop("src","../img/userPortrait/"+data+".jpg");
            $("#head_portrait").prop("src","../img/userPortrait/"+data+".jpg");
        },"text"
    );


    var ename_global;
    //AJAX获取个人信息
    $.post(
        "/personalInformServlet",
        function(data){
            var date = new Date(data.birthday);
            var time = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
            ename_global = data.ename;
            $("#ename").text(data.ename);
            $("#gender").text(data.gender);
            $("#birthday").text(time);
            $("#native_place").text(data.native_place);
            $("#national").text(data.national);
            $("#political").text(data.political);
            $("#health").text(data.health);
            $("#workingtime").text(data.workingtime);
            $("#current_position").text(data.current_position);
            $("#id_card").text(data.id_card);
            $("#current_employer").text(data.current_employer);
            $("#tel").text(data.tel);
            $("#school_major").text(data.school_major);
            $("#degree").text(data.degree);
        }
    );

    //修改个人信息
    $("#edit_submit").click(function(){
        $.post(
            "/editPersonalInformServlet",
            {   "ename":$("#ename").text(),
                "gender":$("#gender").text(),
                "birthday":$("#birthday").text(),
                "native_place":$("#native_place").text(),
                "national":$("#national").text(),
                "political":$("#political").text(),
                "health":$("#health").text(),
                "workingtime":$("#workingtime").text(),
                "current_position":$("#current_position").text(),
                "id_card":$("#id_card").text(),
                "current_employer":$("#current_employer").text(),
                "tel":$("#tel").text(),
                "school_major":$("#school_major").text(),
                "degree":$("#degree").text()
            },
            function (date){
                if(date==="true")
                    layer.msg("修改成功");
                else
                    layer.msg("修改失败，请输出正确的数据",function (){

                    });
            },
        );

    });



    //AJAX获取工资单


    var month_all;
    //按年月查询绑定选择后事件
    layui.use(['layer', 'jquery', 'form'], function () {
        var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form;

        form.on('select(content2_select)', function(data){
            if(data.value == 1){
                $("#searchSessionNum").attr("disabled","true");
                form.render('select');
            }else{
                $("#searchSessionNum").removeAttr("disabled");
                form.render('select');//select是固定写法 不是选择器
                //执行开始
                if($("#query_way").val()==="all"){
                    $("#salary_date_input").css("color","#dddddd");
                    $("#salary_date_input").attr("placeholder","无需输入此项");
                    $("#salary_date_input").attr("disabled","disabled");
                    month_all = true;
                }
                else{
                    $("#salary_date_input").css("color","black");
                    $("#salary_date_input").attr("placeholder","请选择日期");
                    $("#salary_date_input").removeAttr("disabled");
                    month_all = false;
                }
            }
        });
    });
 
    //日期输入
    layui.use('laydate',function(){
        var laydate = layui.laydate;

        laydate.render({
            elem: '#salary_date_input'
            ,type: 'month'
            ,range: true
            ,btns: ['clear', 'confirm']
        })
    });


    //函数 - 查询提交表单验证
    function salary_result() {
        layui.use('table', function () {
            var table = layui.table;
            
            table.render({
                elem: '#table_salary',
                url: 'http://localhost:8080/querySalaryServlet',
                where: {"queryWay":$("#query_way").val(),"queryDate":$("#salary_date_input").val()},
                totalRow: true,
                toolbar: true,
                loading: true,
                page: true,
                cols: [[
                    {field: 'payroll', width: 300, title: '工资单号',totalRowText: '合计'},
                    {field: 'employno', width: 210, title: '员工号'},
                    {field: 'payoff_time', width: 250, title: '发放时间', sort: true},
                    {field: 'salary_number', width: 220, title: '金额', sort: true, totalRow: true}
                ]]
            });

            //  双击弹出工资详单
            table.on('rowDouble(content2_filter_table)', function(obj) {
                var payroll = obj.data.payroll;
                var payoff_time = obj.data.payoff_time;

                $.post(
                    "/payrollDetailServlet",
                    {"payroll":payroll},
                    function (data){

                        var index = layer.open({
                            type: 2, //此处以iframe举例
                            title: ename_global + " 的工资单",
                            area: ['850px', '330px'],
                            offset: 'auto',
                            scrollbar: false,
                            maxmin: true,
                            content: ['detailed_salary.html', 'yes'],
                            anim: 2,    //弹出动画
                            btn: '关闭',
                            closeBtn: 0,
                            shade: 0,
                            moveOut: true,
                            zIndex: layer.zIndex, //置顶
                            success: function (layero) {
                                layer.setTop(layero); //置顶
                                var body = layer.getChildFrame('body', index);
                                //向iframe页面中添加信息
                                body.find("#orderno").text(payroll);
                                body.find("#payoff_time").text(payoff_time);
                                body.find('#ename').text(ename_global);
                                body.find("#base_salary").text(data.base_salary);
                                body.find("#working_time").text(data.working_time);
                                body.find("#base_salary2").text(data.base_salary);
                                body.find("#bonus").text(data.bonus);
                                body.find("#subsidies").text(data.subsidies);
                                body.find("#overtime_salary").text(data.overtime_salary);
                                body.find("#leave_late").text("-"+data.leave_late);
                                body.find("#tax").text("-"+data.tax);
                                body.find("#salary_number").text(data.salary_number);

                            }
                        })
                    }
                );
            });
        })
    }

    //点击查询按钮开始查询
    var submit_salary = false;
    $("#button_query").click(function(){
        if($("#salary_date_input").val().length==0){
            if(month_all)
                submit_salary = true;
            else
                layer.tips('这里是空哒', '#salary_date_input', {
                    tips: 3
                  });
        }
        else
            submit_salary = true;
        if(submit_salary){
            salary_result();
            submit_salary = false;
        }
    });

    setTimeout(function(){
        var s = '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">';
        s += '你好 '+ename_global+' ！<br><br> 欢迎使用 梦联动 人事管理系统 <br><br> 在这里您可以查看您的职位信息、修改您的个人信息，查询您的工资单等 <br><br> 祝您使用愉快！';
        s += '</div>';
        //进入弹出公告
        layer.open({
            type: 1,
            title: false, //不显示标题栏
            closeBtn: false,
            area: '300px;',
            shade: 0.8,
            id: 'LAY_layuipro', //设定一个id，防止重复弹出
            btn: ['进入系统'],
            btnAlign: 'c',
            moveType: 1, //拖拽模式，0或者1
            content: s,
            success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.find('.layui-layer-btn0').attr({});
            }
        });
    },700);
})
